/**
 * 整体架构页面
 * @author wenchuan.li
 * @date 2018/11/30 15:37
 */
define(function () {

    var self,
        menus = [],
        app = win.app
    ;

    function initLayUi() {

        layui.extend({
            setter: "index/config",
            admin: "index/admin",
            view: "index/view"
        }).define(["setter", "admin"], function (exports) {

            var setter = layui.setter, //e
                element = layui.element, //i
                admin = layui.admin, //n
                tabsPage = admin.tabsPage, //t
                view = layui.view, //d

                openTabsPage = function (_url, _title) {
                    var l,
                        b = $("#LAY_app_tabsheader>li"),
                        y = _url.replace(/(^http(s*):)|(\?[\s\S]*$)/g, "");

                    b.each(function (e) {
                        var li = $(this),
                            layid = li.attr("lay-id");
                        if (layid === _url) {
                            l = true;
                            tabsPage.index = e;
                        }
                    });

                    _title = _title || "新标签页";

                    if (setter.pageTabs) {
                        l || ($(appBody).append(['<div class="layadmin-tabsbody-item layui-show">',
                            '<iframe src="' + _url + '" frameborder="0" class="layadmin-iframe"></iframe>', "</div>"
                        ].join("")),
                            tabsPage.index = b.length,
                            element.tabAdd(o, {
                                title: "<span>" + _title + "</span>",
                                id: _url,
                                attr: y
                            }));
                    } else {
                        var u = n.tabsBody(n.tabsPage.index).find(".layadmin-iframe");
                        u[0].contentWindow.location.href = _url
                    }
                    element.tabChange(o, _url);
                    admin.tabsBodyChange(tabsPage.index, {
                        url: _url,
                        text: _title
                    });
                },
                appBody = "#E4j_app_body",//s
                o = "layadmin-layout-tabs";

            admin.screen() < 2 && admin.sideFlexible();

            view().autoRender();
            //我的资料
            admin.events.info = function () {
                Index.info();
            };
            //修改密码
            admin.events.password = function () {
                Index.password();
            };
            //退出
            admin.events.logout = function () {
                Index.logout();
            };

            exports("index", {
                openTabsPage: openTabsPage
            });

            layui.use(['table', 'laydate'], $.noop);
        });
    }

    var Index = app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'index',
                _loadTplsFull: [{
                    dom: 'password', tpl: 'system/password', time: 3000
                }, {
                    dom: 'info', tpl: 'system/info', time: 2000
                }]
            }, win);
//             var tk_index = 0;
//             var xx_num;
//             $(function () {
//                 xx_num = $(".tipfloat").attr("data-num");
//                 tankuan()
//
//                 $(".close").click(function () {
//                     $(".tipfloat").animate({height: "hide"}, 800);
//                     if (tk_index != xx_num) {
//                         setTimeout(tankuan, 1000);
//                     }
//
//                 });
//             })
//
//             function tankuan() {
//                 if (tk_index != xx_num) {
//                     $(".tipfloat").animate({height: "show"}, 800);
//
// //文本输出可删除
//                     $(".xx_nrong").html(tk_index);
//                     tk_index++;
//                 }
//             }
        },
        getMenus: function () {
            app.ajax({
                xurl: 'index/getMenus',
                xTime: '2m',
                ocache: true,
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        var item, _menus = [];
                        datas.forEach(function (v) {
                            if (v.IS_LEAF == '0') {
                                if (item) {
                                    _menus.push(item);
                                }
                                item = {p: v, c: []};
                            } else {
                                item.c.push(v);
                            }
                        });
                        _menus.push(item);
                        _menus.sort(function (a, b) {
                            if (a.p.SORT_NO < b.p.SORT_NO) {
                                return -1;
                            }
                            if (a.p.SORT_NO > b.p.SORT_NO) {
                                return 1;
                            }
                            return 0;
                        });
                        _menus.forEach(function (v) {
                            v.c.sort(function (a, b) {
                                if (a.SORT_NO < b.SORT_NO) {
                                    return -1;
                                }
                                if (a.SORT_NO > b.SORT_NO) {
                                    return 1;
                                }
                                return 0;
                            });
                        });
                        menus.pushArray(_menus);
                    }
                    initLayUi();
                }
            });
        },

        password: function () {
            self._openTplFull({
                tag: 'password',
                index: 0,
                openOpts: {
                    title: '修改密码',
                    area: ['600px', '300px']
                }
            });
        },

        info: function () {
            self._openTplFull({
                tag: 'info',
                index: 1,
                openOpts: {
                    title: '我的资料',
                    area: ['600px', '500px']
                }
            });
        },

        logout: function () {
            app.ajax({
                xurl: 'logout',
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        location.href = g_ctxPath + 'login';
                    }
                }
            });

            app.lStorage.remove('index2FgetMenus');
        },

        _readyBefore: function () {

            self.getMenus();

        },
        _ready: function () { // dom载入完成后执行

            vue = new Vue({
                el: '#E4j_app',
                data: {
                    menus: menus
                }
            });
        },
        _lazy: function () { // 延迟500ms加载
            this._super();
            require(['pc/public/others'], $.noop);
            $('#page_admin').attr('src', g_ctxPath + 'admin');
        }
    });
});